package com.yyzw.activiti.modeler.controller;

import com.yyzw.activiti.modeler.model.DataGridRespVo;
import com.yyzw.activiti.modeler.model.ResponseVo;
import com.yyzw.activiti.modeler.model.SysUser;
import com.yyzw.activiti.modeler.service.RoleService;
import com.yyzw.activiti.modeler.service.UserService;
import com.yyzw.activiti.modeler.util.EncryptUtil;
import com.yyzw.activiti.modeler.util.ImgUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;


@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @RequestMapping(value = "loginpage")
    public ModelAndView loginPage(HttpServletRequest request,ModelAndView modelAndView){
        modelAndView.setViewName("user/login");
        return modelAndView;
    }

    @RequestMapping(value = "login")
    public ResponseVo userLogin(HttpServletRequest request) throws Exception{
        request.getSession().removeAttribute("userSession");
        String userCode = request.getParameter("userCode");
        String password = request.getParameter("password");
        String validationCode = request.getParameter("validationCode");
        String validationCodeSession =(String) request.getSession().getAttribute("validationCode");
        if(!validationCodeSession.equalsIgnoreCase(validationCode)){
             return new ResponseVo(false,"验证码匹配失败");
        }
        SysUser sysUser = userService.userlogin(userCode);
        if(sysUser == null){
            return new ResponseVo(false,"账号不存在");
        }
        String sysUserPassword = sysUser.getPassword();
        if(!sysUserPassword.equals(EncryptUtil.EncryptString(password))){
            return new ResponseVo(false,"密码输入错误");
        }
         request.getSession().setAttribute("userSession",sysUser);
        return new ResponseVo(true,"登录成功");

    }



    public static void main(String[] args) throws Exception{
        String s = EncryptUtil.EncryptString("1");
        System.out.println(s);
    }
    @RequestMapping(value = "regNewUser")
    public ResponseVo userRegister(HttpServletRequest request,SysUser sysUser) throws  Exception{
        sysUser.setCreateTime(new Date());
        sysUser.setStatus("1");//状态为可用
        sysUser.setPassword(EncryptUtil.EncryptString(sysUser.getPassword()));
        boolean regResult = userService.userRegister(sysUser);
        if(regResult){
            return new ResponseVo(true,"注册成功");
        }
        return new ResponseVo(false,"注册失败");
    }

    @RequestMapping(value="validationImg")
    public void getValidationImg(HttpServletRequest request, HttpServletResponse response) throws  Exception{
        ImgUtil imgUtil=new ImgUtil();
        BufferedImage image = imgUtil.getImage();
        String text = imgUtil.getText();
        request.getSession().setAttribute("validationCode", text);
        try {
            imgUtil.output(image, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value="getSysUserByPage")
    public DataGridRespVo getSysUserByPage(HttpServletRequest request, String page, String rows) throws Exception{
        return this.userService.getSysUserByPage(Integer.parseInt(page),Integer.parseInt(rows));
    }


    @RequestMapping(value="addUser")
    public ResponseVo addSysUser(@RequestBody SysUser sysUser,HttpServletRequest request) throws Exception{
        return this.userService.addSysUser(sysUser);
    }
    @RequestMapping(value="updateUser")
    public ResponseVo updateSysUser(@RequestBody SysUser sysUser,HttpServletRequest request) throws Exception{
        return this.userService.updateSysUser(sysUser);
    }

    @RequestMapping(value="delUserById")
    public ResponseVo delSysUser(@Param("userId") int userId, HttpServletRequest request) throws Exception{
        int delResult = this.userService.delSysUser(userId);
        if(delResult>0){
            return new ResponseVo(true,"删除成功");
        }
        return new ResponseVo(false,"删除失败");
    }

    @RequestMapping(value="getUserList")
    public ModelAndView getUserList(HttpServletRequest request,ModelAndView modelAndView) throws Exception{

        modelAndView.addObject("lists",roleService.getSYsRoleList());
        modelAndView.setViewName("user/userList");
        return modelAndView;
    }
}
